package com.summer.main;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

public class CelueTest {

	public static void main(String[] args) {
		P c = new P();
		c.update(CUUUU.Aurl,GetUersData.getData());
		c.update(CUUUU.Burl,GetItemData.getData());
	}
	
	public static class GetUersData{
		public static String getData(){
			return  "Uers";
		}
	}
	
	public static class GetItemData{
		public static int getData(){
			return 2;
		}
	}

	public static class Celue{

		HashMap<String,Method> actionMap = new HashMap<>();

		public Celue(){
			Method[] methods=getClass().getDeclaredMethods();
			for(Method method : methods){
				if(method.isAnnotationPresent(StrategyAnnotation.class)){
					StrategyAnnotation annotation= method.getAnnotation(StrategyAnnotation.class);
					actionMap.put(annotation.event(), method);
				}
			}
		}
		
		public <T>void update(String msg,T data){
			try {
				Method strategyMethod = actionMap.get(msg);
				if(strategyMethod != null)
					strategyMethod.invoke(this, data);
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				e.printStackTrace();
			} 
		}
		
	}

	public static class P extends Celue{
		@StrategyAnnotation(event=CUUUU.Aurl)
		public void itemAction(String data){
			System.out.println("Item");
		}
		@StrategyAnnotation(event=CUUUU.Burl)
		public void uersAction(int data){
			System.out.println("Uers");
		}
	}
	
	@Retention(RetentionPolicy.RUNTIME)
	@Target({ElementType.METHOD})
	public @interface StrategyAnnotation {
		/**
		 * @param 关注事件
		 * **/
		public String event();
	}
	
	public static class CUUUU{
		public static final String Aurl="ddd";
		public static final String Burl="ccc";
	}

}
